<?php
/**
* A simple database connector - MySQL
* @link http://webappkit.net
* @package webappkit
* @subpackage db
* @author J.Ducastel <nospam0@ducastel.name>
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*/

/**
*
* @author J.Ducastel <nospam0@ducastel.name>
* @version
* @todo sqlite, postgresql connectors
*/
class DbLinkTool extends WakTool {

    /**
     * @var array default conf
     */
    var $cfg=array(
        'dblink'=>array(
            'host'=>'localhost'
            ,'db'=>''
            ,'user'=>'root'
            ,'login'=>''
            ,'encoding'=>''));

    var $link;

    /**
     * required to implement iWakTool
     * @param string $kit_id owner kit
     * @param array $cfg configuration data
     * @return DbLinkTool
     */
    function toolFactory($kit_id,$cfg) {
        return new DblinkTool($kit_id,$cfg);
    }

    /**
     * tries to connect to database
     * opens link, select db if defined, set encoding if defined
     * @return bool
     */
    function connect() {
        // trying to connect user to host
        $host=$this->cfg['dblink']['host'];
        $user=$this->cfg['dblink']['user'];
        $pwd=$this->cfg['dblink']['pwd'];
        if (!$this->link=@mysql_connect($host,$user,$pwd)) {
            //trigger_error("dbLink_mysql->connectUser couldn't connect to host ".$this->host." ".strlen($login)."/".strlen($pwd),E_USER_WARNING);
            //throw new Exception("could not connect user $user to host $host");
            return false;
        }
        // selecting db
        $db=$this->cfg['dblink']['db'];
        if (!$this->selectDb($db)) {
            //throw new Exception("could not select db $db");
            // print_r($this);
            return false;
        }
        // setting encoding
        if (isset($this->cfg['dblink']['encoding'])
        and $this->cfg['dblink']['encoding']
        and !$this->setEncoding($this->cfg['dblink']['encoding'])) {
            //throw new Exception("could not set encoding $encoding");
            // print_r($this);
            return false;
        }
        return true;
    }

    /**
     * selects database
     * @param string $db database name
     * @return bool
     */
    function selectDb($db) {
        return @mysql_select_db($db, $this->link);
    }

    /**
    * executes a sql query, return resource
    * @access public
    * @param string $sql sql query
    * @param bool $buffered default to false
    * @return resource
    */
    function & query($sql,$buffered=false) {
        // execute query
        if ($buffered) {
            $r=@mysql_query($sql,$this->link);
        } else {
            $r=@mysql_unbuffered_query($sql,$this->link);
        }
        return $r;
    }

    /**
     * gets a select result object
     * @param string $sql SQL query
     * @return DbResult
     */
    function getResult($sql) {
        return new DbResult($sql);
    }

    /**
     * sets connection encoding
     * @param string $encoding
     * @return bool
     */
    function setEncoding($encoding) {
        // MySQL 4.1
        if (!$this->query("SET NAMES '$encoding'")
        // MySQL 4.0
        and !$this->query("SET CHARACTER SET '$encoding'")) {
            trigger_error("failed to set encoding $encoding : ".mysql_error());
            return false;
        }
        $this->encoding=$encoding;
        return true;
    }
}
